package com.zuo_book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zuo_book.common.BusinessException;
import com.zuo_book.common.ResultCode;
import com.zuo_book.entity.SysProject;
import com.zuo_book.entity.SysProjectMember;
import com.zuo_book.mapper.SysProjectMapper;
import com.zuo_book.mapper.SysProjectMemberMapper;
import com.zuo_book.service.SysProjectMemberService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 项目成员服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysProjectMemberServiceImpl implements SysProjectMemberService {

    private final SysProjectMemberMapper projectMemberMapper;
    private final SysProjectMapper projectMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addMember(SysProjectMember member) {
        // 检查项目是否存在
        SysProject project = projectMapper.selectById(member.getProjectId());
        if (project == null) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED.getCode(), "项目不存在");
        }
        
        // 检查用户是否已经是项目成员
        if (isProjectMember(member.getProjectId(), member.getUserId())) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED.getCode(), "该用户已经是项目成员");
        }
        
        // 设置默认角色类型
        if (member.getRoleType() == null) {
            member.setRoleType(1); // 默认为普通成员
        }
        
        // 插入成员
        projectMemberMapper.insert(member);
        
        return member.getId();
    }

    @Override
    public void updateMember(SysProjectMember member) {
        // 检查成员是否存在
        SysProjectMember existingMember = projectMemberMapper.selectById(member.getId());
        if (existingMember == null) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED.getCode(), "项目成员不存在");
        }
        
        // 更新成员
        projectMemberMapper.updateById(member);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMember(Long memberId) {
        // 检查成员是否存在
        SysProjectMember member = projectMemberMapper.selectById(memberId);
        if (member == null) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED.getCode(), "项目成员不存在");
        }
        
        // 检查是否是创建者
        if (member.getRoleType() != null && member.getRoleType() == 0) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED.getCode(), "不能删除项目创建者");
        }
        
        // 删除成员
        projectMemberMapper.deleteById(memberId);
    }

    @Override
    public SysProjectMember getMemberById(Long memberId) {
        SysProjectMember member = projectMemberMapper.selectById(memberId);
        if (member == null) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED.getCode(), "项目成员不存在");
        }
        return member;
    }

    @Override
    public List<SysProjectMember> getProjectMembers(Long projectId) {
        // 检查项目是否存在
        SysProject project = projectMapper.selectById(projectId);
        if (project == null) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED.getCode(), "项目不存在");
        }
        
        // 查询项目成员
        LambdaQueryWrapper<SysProjectMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysProjectMember::getProjectId, projectId);
        
        return projectMemberMapper.selectList(wrapper);
    }

    @Override
    public Page<SysProjectMember> listProjectMembers(Long projectId, int page, int size) {
        // 检查项目是否存在
        SysProject project = projectMapper.selectById(projectId);
        if (project == null) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED.getCode(), "项目不存在");
        }
        
        // 创建分页对象
        Page<SysProjectMember> pageParam = new Page<>(page, size);
        
        // 构建查询条件
        LambdaQueryWrapper<SysProjectMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysProjectMember::getProjectId, projectId);
        
        // 执行查询
        return projectMemberMapper.selectPage(pageParam, wrapper);
    }

    @Override
    public boolean isProjectMember(Long projectId, Long userId) {
        LambdaQueryWrapper<SysProjectMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysProjectMember::getProjectId, projectId)
               .eq(SysProjectMember::getUserId, userId);
        
        return projectMemberMapper.selectCount(wrapper) > 0;
    }

    @Override
    public Integer getMemberRole(Long projectId, Long userId) {
        LambdaQueryWrapper<SysProjectMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysProjectMember::getProjectId, projectId)
               .eq(SysProjectMember::getUserId, userId);
        
        SysProjectMember member = projectMemberMapper.selectOne(wrapper);
        
        return member != null ? member.getRoleType() : null;
    }
}